package com.flbill.controller;

import com.flbill.common.result.Result;
import com.flbill.common.utils.JwtUtils;
import com.flbill.pojo.dto.LoginDTO;
import com.flbill.pojo.dto.RegisterDTO;
import com.flbill.pojo.vo.AdminVO;
import com.flbill.pojo.vo.LoginVO;
import com.flbill.service.AdminService;
import com.flbill.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
@Slf4j
public class AuthorizationController {

    private final UserService userService;
    private final AdminService adminService;
    private final AdminService adminServiceImpl;

    public AuthorizationController(UserService userService, AdminService adminService, AdminService adminServiceImpl) {
        this.userService = userService;
        this.adminService = adminService;
        this.adminServiceImpl = adminServiceImpl;
    }

    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("登录用户:{}", loginDTO.getUsername());
        LoginVO loginVO = userService.login(loginDTO);
        loginVO.setToken(JwtUtils.generateToken(loginVO.getUserInfo().getId(), loginVO.getUserInfo().getUsername()));
        log.info("\ntoken:{}", loginVO.getToken());
        log.info("登录成功:{}", loginVO.getUserInfo().getUsername());
        return Result.success(loginVO);
    }

    @PostMapping("/register")
    public Result<Void> register(@RequestBody RegisterDTO registerDTO) {
        log.info("registerDTO: {}", registerDTO);
        userService.register(registerDTO);
        return Result.success();
    }

    //管理员登录
    @PostMapping("/admin/login")
    public Result<AdminVO> adminLogin(@RequestBody LoginDTO loginDTO) {
        log.info("管理员:{}", loginDTO.getUsername());
        AdminVO loginVO = adminService.login(loginDTO);
        loginVO.setToken(JwtUtils.generateToken(loginVO.getId(), loginVO.getUsername()));
        log.info("登录成功:{}", loginVO.getUsername());
        return Result.success(loginVO);
    }

} 